package com.bnyy.medicalHousekeeper.moudle.message.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.SizeUtils;
import com.bnyy.medicalHousekeeper.R;
import com.bnyy.medicalHousekeeper.moudle.message.MessageHeaderHelper;
import com.bnyy.medicalHousekeeper.moudle.message.MessageItemFirstLetterHelper;
import com.bnyy.message.bean.Contact;
import com.bnyy.message.bean.ContactGroup;
import com.bnyy.message.bean.ContactUser;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;

/**
 * Author : 关炳鹏
 * Description :
 * Date : 2022/11/9
 **/
public class ContactAdapter extends RecyclerView.Adapter<ContactAdapter.ViewHolder> {

    private int dp12;
    private Context context;
    private LayoutInflater inflater;
    private ArrayList<Contact> target = new ArrayList<>();
//    private HashSet<Integer> selectedContactIds = new HashSet<>();
    //    private boolean selectContact;
//    private int max;
    private boolean singleSelect;
    private Callback callback;

    public ContactAdapter(Context context, boolean singleSelect, Callback callback) {
        this.context = context;
        this.singleSelect = singleSelect;
        this.callback = callback;
        inflater = LayoutInflater.from(context);
        dp12 = SizeUtils.dp2px(12);
    }

    public void refresh(ArrayList<? extends Contact> contacts) {
        refresh(contacts, null, null);
    }

    public void refresh(ArrayList<? extends Contact> contacts, ConstantItem constantItem) {
        refresh(contacts, constantItem, null);
    }

    public void refresh(ArrayList<? extends Contact> contacts, ConstantItem constantItem, HashSet<Integer> selectedContactIds) {
        target.clear();

        if (constantItem != null) {
            target.add(constantItem);
        }

        ArrayList<String> firstLetters = new ArrayList<>();
        HashMap<String, ArrayList<Contact>> map = new HashMap<>();
        map.put("#", new ArrayList<>());
        for (Contact contact : contacts) {
            if (selectedContactIds != null && selectedContactIds.size() > 0) {
                if (selectedContactIds.contains(contact.getId())) {
                    contact.setChecked(true);
                }
            }
            String name = "";
            if (contact instanceof ContactGroup) {
                name = ((ContactGroup) contact).getGroup_name();
            } else {
                name = contact.getUsername();
            }

            if (TextUtils.isEmpty(name)) {
                map.get(MessageItemFirstLetterHelper.UNKONWN).add(contact);
            } else {
                String firstLetter = MessageItemFirstLetterHelper.getFirstLetter(name.charAt(0));
                if (!firstLetters.contains(firstLetter)) {
                    firstLetters.add(firstLetter);
                }
                if (map.containsKey(firstLetter)) {
                    map.get(firstLetter).add(contact);
                } else {
                    ArrayList<Contact> value = new ArrayList<>();
                    value.add(contact);
                    map.put(firstLetter, value);
                }
            }
        }

        for (char c : MessageItemFirstLetterHelper.FIRST_LETTER) {
            String s = String.valueOf(c);

            ArrayList<Contact> list = map.get(s);
            if (list != null && list.size() > 0) {
                target.add(new FirstLetterItem(s));
                target.addAll(list);
            }
        }

        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        switch (viewType) {
            case 1:
                return new ConstantViewHolder(inflater.inflate(R.layout.item_message_contact_header, parent, false));
            case 2:
                TextView tv = new TextView(context);
                tv.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
                tv.setBackgroundColor(ContextCompat.getColor(context, R.color.gray_FA));
                tv.setPadding(dp12, dp12 / 2, dp12, dp12 / 2);
                return new FirstLetterHolder(tv);
        }
        return new ContactViewHolder(inflater.inflate(R.layout.item_message_contact, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Contact contact = target.get(position);
        if (contact instanceof ConstantItem) {
            ConstantViewHolder constantViewHolder = (ConstantViewHolder) holder;
            ConstantItem constantItem = (ConstantItem) contact;
            constantViewHolder.tvTitle.setVisibility(TextUtils.isEmpty(constantItem.getTitle()) ? View.GONE : View.VISIBLE);
            constantViewHolder.tvTitle.setText(constantItem.getTitle());

            constantViewHolder.llConstants.removeAllViews();

            for (Constant constant : constantItem.constants) {
                View item = inflater.inflate(R.layout.item_message_contact_constant, constantViewHolder.llConstants, false);

                TextView tvType = item.findViewById(R.id.tv_type);
                tvType.setText(constant.name);

                RelativeLayout rlHeaders = item.findViewById(R.id.rl_headers);
                MessageHeaderHelper.setHeader(context, rlHeaders, constant.imageResId);
//                ImageView iv = item.findViewById(R.id.iv);
//                iv.setImageResource(constant.imageResId);
                item.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (callback != null) {
                            callback.onConstantItemClick(constant);
                        }
                    }
                });
                constantViewHolder.llConstants.addView(item);
            }
        } else if (contact instanceof FirstLetterItem) {
            FirstLetterHolder firstLetterHolder = (FirstLetterHolder) holder;
            firstLetterHolder.tvFirstLetter.setText(((FirstLetterItem) contact).firstLetter);
        } else {
            ContactViewHolder contactViewHolder = (ContactViewHolder) holder;

            if (contact instanceof ContactGroup) {
                ContactGroup contactGroup = (ContactGroup) contact;
                contactViewHolder.tvName.setText(contactGroup.getGroup_name());
                MessageHeaderHelper.setHeader(context, contactViewHolder.rlHeaders, contactGroup.getImgs());
            } else if (contact instanceof ContactUser) {
                contactViewHolder.tvName.setText(contact.getUsername());
                MessageHeaderHelper.setHeader(context, contactViewHolder.rlHeaders, contact.getUser_img());
            }

            if (singleSelect) {
                contactViewHolder.cb.setVisibility(View.GONE);
            } else {
                contactViewHolder.cb.setVisibility(View.VISIBLE);
                contactViewHolder.cb.setChecked(contact.isChecked());
                contactViewHolder.cb.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        boolean checked = contactViewHolder.cb.isChecked();
                        contact.setChecked(checked);
                        if (callback != null) {
                            callback.onCheck(contact, checked, position);
                        }
                    }
                });
            }
            contactViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (!singleSelect) {
                        contactViewHolder.cb.performClick();
                    }

                    if (callback != null) {
                        callback.onItemClick(contact, position);
                    }
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return target.size();
    }

    @Override
    public int getItemViewType(int position) {
        Contact contact = target.get(position);
        if (contact instanceof ConstantItem) {
            return 1;
        } else if (contact instanceof FirstLetterItem) {
            return 2;
        } else {
            return super.getItemViewType(position);
        }
    }


    public static class ViewHolder extends RecyclerView.ViewHolder {
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
        }
    }

    public static class ContactViewHolder extends ContactAdapter.ViewHolder {
        TextView tvName;
        RelativeLayout rlHeaders;
        CheckBox cb;

        public ContactViewHolder(@NonNull View itemView) {
            super(itemView);
            tvName = itemView.findViewById(R.id.tv_name);
            rlHeaders = itemView.findViewById(R.id.rl_headers);
            cb = itemView.findViewById(R.id.cb);
        }
    }

    public static class ConstantViewHolder extends ContactAdapter.ViewHolder {

        LinearLayout llConstants;
        TextView tvTitle;

        public ConstantViewHolder(@NonNull View itemView) {
            super(itemView);
            llConstants = itemView.findViewById(R.id.ll_constants);
            tvTitle = itemView.findViewById(R.id.tv_title);
        }
    }

    public static class FirstLetterHolder extends ContactAdapter.ViewHolder {
        TextView tvFirstLetter;

        public FirstLetterHolder(@NonNull View itemView) {
            super(itemView);
            tvFirstLetter = (TextView) itemView;
        }
    }

    public static class FirstLetterItem extends Contact {
        private String firstLetter;

        public FirstLetterItem(String firstLetter) {
            this.firstLetter = firstLetter;
        }
    }

    public static class ConstantItem extends Contact {
        private Constant[] constants;
        private String title = "";

        public ConstantItem(Constant... constants) {
            this.constants = constants;
        }

        public ConstantItem(String title, Constant... constants) {
            this.title = title;
            this.constants = constants;
        }

        public Constant[] getConstants() {
            return constants;
        }

        public void setConstants(Constant[] constants) {
            this.constants = constants;
        }

        public String getTitle() {
            return title;
        }

        public void setTitle(String title) {
            this.title = title;
        }
    }

    public enum Constant implements Serializable {

        FAMILY("我的家人", R.mipmap.img_message_family),
        GROUP("我的群聊", R.mipmap.img_message_group);

        private String name;
        private int imageResId;

        Constant(String name, int header) {
            this.name = name;
            this.imageResId = header;
        }

        public String getName() {
            return name;
        }
    }

    public static class CallbackImpl implements Callback {

        @Override
        public void onItemClick(Contact contact, int position) {

        }

        @Override
        public void onConstantItemClick(Constant constant) {

        }

        @Override
        public void onCheck(Contact contact, boolean checked, int position) {

        }
    }

    public interface Callback {
        void onItemClick(Contact contact, int position);

        void onConstantItemClick(Constant constant);

        void onCheck(Contact contact, boolean checked, int position);
    }
}




























